const path = require("path");
const Express = require("express");
const bodyParser = require("body-parser");
const App = Express();
const HtmlToPug= require("./lib/lib");
const fs = require("fs");
const {AsyncWriteFile,CloneData} = require("./utils");


App.set("view engine","ejs");
App.set("views",path.join("views"));

// parse application/x-www-form-urlencoded
App.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
App.use(bodyParser.json())

App.get("/", function (req,res){
    res.render("index")
})

App.post("/api/getHtml",async (req,res)=>{
    let t1 = new HtmlToPug(req.body.html);
    await Promise.all([
        AsyncWriteFile("./test.pug",t1.text),
        AsyncWriteFile("./test.json",JSON.stringify(CloneData(t1.tree))),
    ])
    res.send({code:1,msg:"",data: t1.text})
})


App.listen(3000,function () {
    console.log("localhost:3000")
})
